package com.yugao.fintech.draper.security.handler;

import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.core.util.WebUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 资源服务器异常主要有两种：令牌不正确返回401和用户无权限返回403。
 * 因为资源服务器有多个，所以相关的异常处理类可以定义在draper-starter通用模块里。
 * <p>
 * 用于处理401类型异常
 * </p>
 */
public class UrlAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        WebUtil.makeResponse(response, MediaType.APPLICATION_JSON_VALUE, HttpServletResponse.SC_UNAUTHORIZED,
                ResultVO.error("token无效"));
    }
}
